home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / demo / mag / Eurochart36a.lha / eurochart36 / Articles / Something < prev    next >
Text File  |  1999-02-02  |  13KB  |  781 lines

  1. »CL5:--------------------------------------
  2. »CL4:         »BIG:The simple starfield»SML:
  3. »CL5:--------------------------------------
  4.  
  5. »CL1:           by Cytron/Depth
  6.  
  7. »CL0:I'm kind of uninspired for the time
  8. being. The last effect, I decided to
  9. make, was a faked starroutine.
  10.  
  11. »CL1:»BIG:The concept:
  12. »SML:»CL0:
  13. »CL1:-» Precalc some tables for the movement
  14.   of each star.
  15.   (I'll get back to this part later)
  16. »CL1:-» Shut down system.
  17. »CL1:-» Plot starmovement of each star,
  18.   every star's startingframe varying
  19.   from the next.
  20.  
  21. It's as simple as that :-)
  22.  
  23. »CL2:When making complex routines, it is
  24. important to divide it into smaller
  25. parts. This is not a complex routine,
  26. but anyhow, I find it very easier to
  27. keep my overview when dividing into
  28. smaller parts. This makes my code
  29. sadly unoptimized, but optimizing is
  30. not recommended for beginners anyway
  31. :-)  - first of all, a coder needs to
  32. think normally - later on in his/her
  33. carreer he/she can then turn into an
  34. incredibly skilled optimizer and join
  35. groups like Oxyron and EndZeit... This
  36. is way beyond the scope of this
  37. article, however. Let's get it on:
  38.  
  39. »CL4:For each star, I've calculated 128
  40. frames of movement from (0,0) to a
  41. random point on the circle centering
  42. (0,0) and having radius 170 - just
  43. outside the boundaries on the screen
  44. (okok - the distance from the center
  45. to the corners is 205, but noone will
  46. ever notice).
  47.  
  48. »CL3:»BIG:The math:
  49. »CL2:»SML:
  50. Interpolating (meaning taking the
  51. straight line between two points)
  52. linearly (having the same deltavalue
  53. for each entry) will look wrong.
  54.  
  55. Explaining this is a bit hard for me,
  56. but study your own physics/math-books
  57. if you want to know why.
  58.  
  59. Instead, a parabolic interpolation
  60. will look right. In straight
  61. mathematical terms, the movement will
  62. look like this:
  63.  
  64. »CL3:x(n) = ScaleXFactor*n²
  65. y(n) = ScaleYFactor*n²
  66. »
  67. , where n will range from 0 to 127.
  68.  
  69. For each point, I will need to
  70. calculate the X and Y scalefactor.
  71.  
  72. This isn't all that hard, as I know
  73. that the last frame has n=127, which
  74. gives me:
  75. »CL3:
  76. EndPointX = x(127) = ScaleXFactor*127²
  77. EndPointY = y(127) = ScaleYFactor*127²
  78. »
  79. and since both EndPointX and EndPointY
  80. are known values, the scalefactors
  81. will be
  82. »CL3:
  83. ScaleXFactor=EndPointX/127²
  84. ScaleYFactor=EndPointY/127²
  85. »
  86. Or more generally, if the startpoint
  87. isn't (0,0) and the number of frames
  88. is not 128:
  89. »CL3:
  90. XFactor=(EndX-StartX)/(NUMFRAMES-1)²
  91. YFactor=(EndY-StartY)/(NUMFRAMES-1)²
  92. »
  93. and the formulas
  94. »CL3:x(n)=StartX+XFactor*n²
  95. y(n)=StartY+YFactor*n²
  96. »give me the movement.
  97.  
  98.  
  99. »CL7:If this seems as high math for you, I
  100. guess that you'll have to study harder
  101. :-) .. Anyway, in order to be nice,
  102. I've also included a little math on
  103. how to implement the linear
  104. interpolation:
  105.  
  106. Here, the formulas will look like this:
  107. »CL6:XPerFrame=(EndX-StartX)/(NUMFRAMES-1)
  108. YPerFrame=(EndY-StartY)/(NUMFRAMES-1)
  109. »
  110. For each frame, you then simply add
  111. the XPerFrame to the current position.
  112. eg. if x has to move from 0 to 100 in
  113. ten frames, the position will be:
  114. 0, 11, 22, 33, 44, 56, 67, 78, 89, 100
  115.  
  116. »CL8:»BIG:Fixed point representation:
  117. »SML:»CL9:
  118. I'm afraid that my memory let's me
  119. down here - I can't remember if I've
  120. introduced this subject before. Anyway
  121. - fixed point notation comes in very
  122. handy in situations like the above
  123. calculations in machinecode.
  124.  
  125. Without FPU, the registers available
  126. are only able to store whole numbers.
  127. However, the small example above with
  128. interpolation from 0 to 100 with
  129. (EndX-StartX)/(NUMFRAMES-1)
  130. demanded that XPerFrame was
  131. (100-0)/9=11.11
  132.  
  133. This is not that hard to obtain.
  134.  
  135. Before dividing 100 with 9, I simply
  136. multiply it with 2^16. (Easily done
  137. with the swap instruction)
  138.  
  139. This way, the result of the division
  140. is not 11, but 728177. The term for
  141. this is that 11.11 is represented with
  142. 16bit fixed point notation. Adding 1
  143. to the number will be the same as
  144. adding 1/(2^16) to 11.
  145.  
  146. »CL8:When swapping   728177, I get 11
  147. When swapping 2*728177, I get 22
  148. When swapping 9*728177, I get 99
  149. »
  150. Hmm... This hasn't taken me anywhere.
  151. However, adding 0.5 - (represented as
  152. 16bit fixed point it is 32768) to all
  153. results, will give me what I need -
  154. correct rounding.
  155.  
  156. »CL1:»BIG:The point calculation routine:
  157. »SML:»CL0:
  158. I've chosen to have a sine and cosine
  159. table with amplitude 170 and 1024
  160. angles precalced. This way, getting a
  161. random point on the circle boils down
  162. to this:
  163.  
  164. »CL1:-» Get a random number.
  165. »CL1:-» AND with 1023, so the number is in
  166.   the range [0;1023]
  167. »CL1:-» Look up sine to the number. This is
  168.   the YEnd
  169. »CL1:-» Look up cosine to the number. This
  170.   is the XEnd
  171. »CL1:-» Call the subroutine that
  172.   interpolates parabolic.
  173.  
  174. This has to be done NUMPOINTS times.
  175.  
  176. The subroutines interpolating the
  177. points performs the above described
  178. math and calls a subroutine NUMFRAMES
  179. times, that places the point in the
  180. table.
  181. The subroutine placing the point in
  182. the table performs an out of bound
  183. check. If point is out of bounds (0,0)
  184. is put into pointtable instead of the
  185. point.
  186.  
  187. This subdividing comes in handier and
  188. handier, the more complex your
  189. routines get. Why? - you may ask.
  190. Simply because you can TEST each
  191. little routine seperately: Instead of
  192. working through 2000 lines of code one
  193. time for 160 hours to find some
  194. strange error, you can simply make a
  195. little testroutine after finishing
  196. every little subroutine.
  197.  
  198.  
  199.  
  200.  
  201.  
  202.         »PIC:ID1»
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.                         Let's see
  237.                         the listing:
  238.                         First the
  239.                         parabolic
  240.                         precalccaller:
  241. »CL4:InitStars01:
  242. »CL5:;     NUMPOINTS times:
  243. ;     Get a random angle.
  244. ;     Use sines/cosines to get point on circle with radius=RADIUS
  245. ;     Call CalcPoints with (0,0) - (PointX,PointY), NUMFRAMES-1»
  246.       lea      StarData01,a0
  247.       move.w   #SS_NUMPOINTS-1,d7
  248. .loop jsr      Random
  249.       and.w    #1023,d0
  250.       lea      SS_Sine,a1
  251.       lea      256*2(a1),a2
  252.       moveq    #0,d2
  253.       moveq    #0,d3
  254.  
  255.       move.w   (a1,d0.w*2),d4
  256.       move.w   (a2,d0.w*2),d5
  257.       move.w   #NUMFRAMES-1,d6
  258.       jsr      MakeLinePointsParabolic
  259.       dbf      d7,.loop
  260.       rts
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281. »CL9:StarData01 is the memoryarray for the stars.
  282.  
  283.  
  284.  »PIC:Mattarazzo2»
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320. Now the linear precalccaller:
  321. »CL4:InitStars00:
  322. »CL5:;     NUMPOINTS times:
  323. ;     Get a random angle.
  324. ;     Use sines/cosines to get point on circle with radius=RADIUS
  325. ;     Call CalcPoints with (0,0) - (PointX,PointY), NUMFRAMES-1»
  326.       lea      StarData00,a0
  327.       move.w   #SS_NUMPOINTS-1,d7
  328. .loop jsr      Random
  329.       and.w    #1023,d0
  330.       lea      SS_Sine,a1
  331.       lea      256*2(a1),a2
  332.       moveq    #0,d2
  333.       moveq    #0,d3
  334.  
  335.       move.w   (a1,d0.w*2),d4
  336.       move.w   (a2,d0.w*2),d5
  337.       move.w   #NUMFRAMES-1,d6
  338.       jsr      MakeLinePoints
  339.       dbf      d7,.loop
  340.       rts
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361. »CL9:Note that these routines are almost
  362. identical. I could have made ONE
  363. routine, but this seems more clear to me.
  364. The Random routine returns a 'random'
  365. number in d0. More about random
  366. routines in another course :-) 
  367.  
  368.        »PIC:Budgie5»
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. The parabolic inplementer:
  401. »CL4:      section  CalcPoints,code
  402.  
  403. »CL5:; Makes a parabolic looked up table. eg startx=0 endx=100 numframes=10
  404. ; will give 0,1,4,11,19,30,44,60,79,99
  405.  
  406. ; Maths: gets deltax and deltay. divides results by (NUMFRAMES-1)²
  407. ; This result will be multiplyed with framenumber² for each frame.
  408. ; f(t)=scalefactor*t²
  409.  
  410. ; (d2,d3) is (FromX,FromY) d4,d5 is (ToX,ToY).
  411. ; d6 is number of steps-1.
  412. ; a0 is pointer to put points into
  413.  
  414. ; If point is not on screen, (0,0) will be written into memory
  415.  
  416. ; on exit regs contains:
  417. ; a0 pointer to next place in memory
  418. ; d7 not changed
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441. »CL4:MakeLinePointsParabolic:
  442.       move.w   d2,d0
  443.       move.w   d3,d1
  444.       sub.w    d2,d4»CL5:    ; Delta X»
  445.       sub.w    d3,d5»CL5:    ; Delta Y»
  446.       swap     d2
  447.       swap     d3
  448.       swap     d4
  449.       swap     d5
  450.       move.w   #$8000,d2»CL5:        ; XPos±0.5 16 bit fixed point»
  451.       move.w   #$8000,d3»CL5:        ; YPos±0.5 16 bit fixed point»
  452.  
  453.       move.l   d2,a2
  454.       move.l   d3,a3
  455.       clr.w    d4
  456.       clr.w    d5
  457.       move.w   d6,d0
  458.       mulu.w   d0,d0
  459.       divs.l   d0,d4»CL5:    ; Scale X 16bit fixed point»
  460.       divs.l   d0,d5»CL5:    ; Scale Y 16bit fixed point»
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.  
  471.  
  472.  
  473.  
  474.  
  475.  
  476.  
  477.  
  478.  
  479.  
  480.  
  481.       moveq    #0,d2»CL5:                    ; Framecount»
  482.       bra.b    .firsttimejump
  483. .loop move.w   d2,d0
  484.       mulu.w   d0,d0»CL5:                    ; n²»
  485.       move.l   d0,d1»CL5:                    ; n²»
  486.       muls.l   d4,d0
  487.       muls.l   d5,d1
  488.       add.l    a2,d0»CL5:                    ; Next XPos»
  489.       add.l    a3,d1»CL5:                    ; Next YPos»
  490.       swap     d0   »CL5:                    ; Correct from 16bit fixedpoint»
  491.       swap     d1
  492. .firsttimejump      »CL5:                    ; First time, just insert startpos»
  493.       bsr      InsertPoint
  494.       addq.w   #1,d2
  495.       dbf      d6,.loop
  496.       rts
  497.       
  498.  
  499.  
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521. »CL9:And the linear variant:
  522.  
  523. »CL4:      section  CalcPoints,code
  524.  
  525. »CL5:; (d2,d3) is (FromX,FromY) d4,d5 is (ToX,ToY).
  526. ; d6 is number of steps-1.
  527. ; a0 is pointer to put points into
  528. ; If point is not on screen, (0,0) will be written into memory
  529.  
  530. ; on exit regs contains:
  531. ; a0 pointer to next place in memory
  532. ; d7 not changed»
  533. MakeLinePoints:
  534.       move.w   d2,d0
  535.       move.w   d3,d1
  536.       sub.w    d2,d4    »CL5:; Delta X»
  537.       sub.w    d3,d5    »CL5:; Delta Y»
  538.       swap     d2
  539.       swap     d3
  540.       swap     d4
  541.  
  542.  
  543.  
  544.  
  545.  
  546.  
  547.  
  548.  
  549.  
  550.  
  551.  
  552.  
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561. »CL4:      swap     d5
  562.       clr.w    d2»CL5:       ; XPos 16 bit fixed point»
  563.       clr.w    d3»CL5:       ; XPos 16 bit fixed point»
  564.       clr.w    d4
  565.       clr.w    d5
  566.       divs.l   #NUMFRAMES-1,d4    »CL5:  ; Step X 16bit fixed point»
  567.       divs.l   #NUMFRAMES-1,d5    »CL5:  ; Step Y 16bit fixed point»
  568.       bra.b    .firsttimejump
  569. .loop add.l    d4,d2              »CL5:      ; Next XPos»
  570.       add.l    d5,d3              »CL5:      ; Next YPos»
  571.       move.l   d2,d0              »CL5:      ; Correct from 16bit fixedpoint»
  572.       move.l   d3,d1
  573.       swap     d0
  574.       swap     d1
  575. .firsttimejump                    »CL5:      ; First time, just insert startpos»
  576.       bsr      InsertPoint
  577.       dbf      d6,.loop
  578.       rts
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.  
  599.  
  600.  
  601. »CL9:Note that both routines call the subroutine InsertPoint:
  602.  
  603. »CL5:; (d0,d1) is (x,y) to move into (a0) with increment of a0
  604. ; If point is not on screen, (0,0) will be written into memory
  605. »CL4:InsertPoint:
  606.       cmp.w    #XMAX,d1
  607.       bgt.b    .outofbounds
  608.       cmp.w    #XMIN,d1
  609.       blt.b    .outofbounds
  610.       cmp.w    #YMAX,d0
  611.       bgt.b    .outofbounds
  612.       cmp.w    #YMIN,d0
  613.       blt.b    .outofbounds
  614.       swap     d0
  615.       move.w   d1,d0
  616.       move.l   d0,(a0)+
  617.       rts
  618. .outofbounds
  619.       move.l   #0,(a0)+
  620.       rts
  621.  
  622.  
  623.  
  624.  
  625.  
  626.  
  627.  
  628.  
  629.  
  630.  
  631.  
  632.  
  633.  
  634.  
  635.  
  636.  
  637.  
  638.  
  639.  
  640.  
  641. »CL0:I should perhaps note, that this
  642. listing has been contained in four
  643. different files.
  644.  
  645. This is another great idea when going
  646. upwards in complexity - make a lot a
  647. general routines, so that yu don't
  648. have to copy/paste all your code from
  649. one time to another - but rather just
  650. include the needed routines and call
  651. them.
  652.  
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660. »CL9:Plotting is now obtained like this:
  661. »PIC:Budgie6»
  662.  
  663.  
  664.  
  665.  
  666.  
  667.  
  668.  
  669.  
  670.  
  671.  
  672.  
  673.  
  674.  
  675.  
  676.  
  677.  
  678.  
  679.  
  680.  
  681.  
  682. »CL4:SS_StarData:
  683. »CL5:; Point at FramePos d0   in point 1
  684. ; Point at FramePos d0+1 in point 2
  685. ; Point at FramePos d0+2 in point 3 etc.
  686. ; ArraySize for each pointdata is 2*2*128 (128 is numframes)
  687. »
  688.       move.l   a4,a0
  689.       lea      SS_Points,a1
  690.       move.w   #SS_NUMPOINTS-1,d7
  691. .loop and.w    #$007f,d0                »CL5:; Wrap counter around at 128»
  692.       move.l   (a0,d0.w*4),(a1)+        »CL5:; *4 as each Pointentry is a longword»
  693.       addq.w   #1,d0                    »CL5:; Index next Frame in next point»
  694.       add.l    #2*2*NUMFRAMES,a0        »CL5:; Index Next Point»
  695.       dbf      d7,.loop
  696.       rts
  697.  
  698.  
  699.  
  700.  
  701.  
  702.  
  703.  
  704.  
  705.  
  706.  
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719. »CL0:This routine is called with two
  720. parameters:
  721. »CL1:d0» contains current framenumber
  722. »CL1:a4» contains pointer to the points -
  723. either the parabolic or the linear
  724. interpolation.
  725.  
  726. »CL1:SS_Points» is the pointer to the points
  727. that will actually be displayed on the
  728. screen.
  729.  
  730. This is done by yet another routine -
  731. my planarplotter that I have written
  732. about in another course.
  733.  
  734. The planarplotter is called with a
  735. pointer to the following structure:
  736.  
  737. »CL5:;DC.W Numpoints
  738. ;DC.W PlScrW (40 for 320)
  739. ;DC.W Height
  740. ;DC.L Pointer to Pointer to PlScrDest
  741. ;DC.L PointSrc (Y.W,X.W Signed)
  742. »
  743. Confused yet? Well - nobody ever said
  744. it was easy to code :-) Anyway - those
  745. of you out there that actually
  746. understand all this and get something
  747. out of it (all you newbies, or
  748. whatever you have turned into by now)
  749. might begin to wonder: How come that
  750. he hasn't made his precalc routines
  751. more general - called with a poitner
  752. containing:
  753. »CL5:
  754. ;DC.W Numpoints
  755. ;DC.W NumFrames
  756. ;DC.W XMin
  757. ;DC.W XMax
  758. ;DC.W YMin
  759. ;DC.W YMax
  760. ;DC.W NumAngles
  761. ;DC.W Radius
  762. ;DC.L Pointer to PointData
  763. »
  764. This is simply because I'm lazy - and
  765. I really ought to rewrite it and make
  766. it like that instead - it will be a
  767. little bit slower and the size will
  768. grow. However, it would make my
  769. routine more flexible and faster to
  770. change.
  771.  
  772.  
  773. »BIG:»CL6:Epilogue:
  774. »SML:»CL7:
  775. As you'll notice in the example
  776. source, I've also included star trails
  777. and fadeups/fadedowns. I'm sure that
  778. some of you'd like to know how - well
  779. - go figure, and perhaps I'll tell you
  780. next time :-)
  781. »PIC:Mattarazzo3»